package com.wmssystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wmssystem.dto.Result;
import com.wmssystem.entity.ReceiveMaterial;
import com.wmssystem.service.impl.ReceiveMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("receiveMaterial")
@ResponseBody
public class ReceiveMaterialController {
    @Autowired
    ReceiveMaterialService receiveMaterialService;

    @PostMapping("increase")
    public Result increase(@RequestBody ReceiveMaterial receiveMaterial) {
        receiveMaterial.setStatus("未完成");
        receiveMaterialService.save(receiveMaterial);
        return Result.ok("添加成功");
    }

    @PostMapping("update")
    public Result update(@RequestBody ReceiveMaterial receiveMaterial) {
        receiveMaterialService.updateById(receiveMaterial);
        return Result.ok("更新成功");
    }

    @DeleteMapping("delete")
    public Result delete(Long receiveMaterialId) {
        receiveMaterialService.removeById(receiveMaterialId);
        return Result.ok("删除成功");
    }

    @GetMapping("query")
    public Result query(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<ReceiveMaterial> receiveMaterialPage = new Page<>(page,size);
        receiveMaterialService.page(receiveMaterialPage, new QueryWrapper<ReceiveMaterial>().orderByAsc("receive_material_id"));
        List<ReceiveMaterial> receiveMaterials = receiveMaterialPage.getRecords();
        return Result.ok(receiveMaterials);
    }
}
